package com.example.service.impl;


import com.example.mapper.AdminMapper;
import com.example.model.Admin;
import com.example.service.UserService;
import com.example.util.DataMap;
import com.example.util.ErrorStatusEnum;
import com.example.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tao95
 * Date: 2017/5/8
 * Time: 15:40
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private AdminMapper adminMapper;
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public DataMap findUser(String queryParam,String password) {
        DataMap dataMap = new DataMap();
        Admin admin = adminMapper.findAdmin(queryParam);
        if(admin == null){
            dataMap.addAttribute("code", ErrorStatusEnum.USER_NOT_IEXTS.getErrorCode());
        }
        password = MD5Util.encodeToHex(password);
        if(password.equals(admin.getPassword())){
            dataMap.addAttribute("code",ErrorStatusEnum.SUCCESS.getErrorCode());
            dataMap.addAttribute("admin",admin);
        }else{
            dataMap.addAttribute("code",ErrorStatusEnum.PASSWORD_ERROR.getErrorCode());
        }
        return dataMap;
    }

    @Override
    @Transactional
    public DataMap repeatPass(Admin admin, String oldPass, String newPass) {
        DataMap dataMap = new DataMap();
        if(MD5Util.encodeToHex(oldPass).equals(admin.getPassword())){
            //TODO
            admin.setPassword(MD5Util.encodeToHex(newPass));
            try {
                adminMapper.updataPassById(admin);
            }catch(Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.EDITPASSWPRD_FAILED.getErrorCode());
                return dataMap;
            }
            dataMap.put("code",ErrorStatusEnum.EDITPASSWPRD_SUCCESS.getErrorCode());
        }else{
            dataMap.put("code", ErrorStatusEnum.PASSWORD_ERROR.getErrorCode());
        }
        return dataMap;
    }

    @Override
    @Transactional
    public DataMap insertAdmin(Admin admin) {
        DataMap dataMap = new DataMap();
        if(admin.getId() == null){
            admin.setPassword(MD5Util.encodeToHex("123456"));
            try {
                adminMapper.insertAdmin(admin);
            }catch(Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.ADD_ADMINFAIL.getErrorCode());
                return dataMap;
            }
        }else{
            try{
                adminMapper.editAdmin(admin);
            }catch (Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.EDIT_ADMINFAIL.getErrorCode());
                return dataMap;
            }
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }

    @Override
    public DataMap selectAdminList() {
        DataMap dataMap = new DataMap();
        List<Admin> adminList = adminMapper.selectAdminList();
        dataMap.addAttribute("adminList",adminList);
        return dataMap;
    }

    @Override
    @Transactional
    public DataMap delAdmin(Integer id) {
        DataMap dataMap = new DataMap();
        try{
            adminMapper.delAdmin(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            dataMap.put("code", ErrorStatusEnum.DEL_ADMINFAIL.getErrorCode());
            return dataMap;
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }

}